import request from '@/utils/request'
import type { 
  ApiResponse, 
  PageData, 
  UserInfo, 
  BoardPost, 
  JobPost, 
  LostPost, 
  LeavePost, 
  NewsPost, 
  Comment,
  SearchParams 
} from '@/types'

// 用户相关API
export const userApi = {
  // 获取用户信息
  getUserInfo(): Promise<ApiResponse<UserInfo>> {
    return request.get('/user/info')
  },
  
  // 更新用户信息
  updateUserInfo(data: Partial<UserInfo>): Promise<ApiResponse> {
    return request.put('/user/info', data)
  },
  
  // 用户登录
  login(data: { mobile: string; code: string }): Promise<ApiResponse<{ token: string; userInfo: UserInfo }>> {
    return request.post('/user/login', data)
  },
  
  // 发送验证码
  sendCode(mobile: string): Promise<ApiResponse> {
    return request.post('/user/send-code', { mobile })
  }
}

// 表白墙API
export const boardApi = {
  // 获取表白墙列表
  getList(params: SearchParams): Promise<ApiResponse<PageData<BoardPost>>> {
    return request.get('/board/list', { params })
  },
  
  // 获取表白详情
  getDetail(id: string): Promise<ApiResponse<BoardPost>> {
    return request.get(`/board/detail/${id}`)
  },
  
  // 发布表白
  create(data: Partial<BoardPost>): Promise<ApiResponse> {
    return request.post('/board/create', data)
  },
  
  // 更新表白
  update(id: string, data: Partial<BoardPost>): Promise<ApiResponse> {
    return request.put(`/board/update/${id}`, data)
  },
  
  // 删除表白
  delete(id: string): Promise<ApiResponse> {
    return request.delete(`/board/delete/${id}`)
  }
}

// 兼职招聘API
export const jobApi = {
  // 获取兼职列表
  getList(params: SearchParams): Promise<ApiResponse<PageData<JobPost>>> {
    return request.get('/job/list', { params })
  },
  
  // 获取兼职详情
  getDetail(id: string): Promise<ApiResponse<JobPost>> {
    return request.get(`/job/detail/${id}`)
  },
  
  // 发布兼职
  create(data: Partial<JobPost>): Promise<ApiResponse> {
    return request.post('/job/create', data)
  },
  
  // 更新兼职
  update(id: string, data: Partial<JobPost>): Promise<ApiResponse> {
    return request.put(`/job/update/${id}`, data)
  },
  
  // 删除兼职
  delete(id: string): Promise<ApiResponse> {
    return request.delete(`/job/delete/${id}`)
  }
}

// 失物招领API
export const lostApi = {
  // 获取失物招领列表
  getList(params: SearchParams): Promise<ApiResponse<PageData<LostPost>>> {
    return request.get('/lost/list', { params })
  },
  
  // 获取失物招领详情
  getDetail(id: string): Promise<ApiResponse<LostPost>> {
    return request.get(`/lost/detail/${id}`)
  },
  
  // 发布失物招领
  create(data: Partial<LostPost>): Promise<ApiResponse> {
    return request.post('/lost/create', data)
  },
  
  // 更新失物招领
  update(id: string, data: Partial<LostPost>): Promise<ApiResponse> {
    return request.put(`/lost/update/${id}`, data)
  },
  
  // 删除失物招领
  delete(id: string): Promise<ApiResponse> {
    return request.delete(`/lost/delete/${id}`)
  }
}

// 闲置买卖API
export const leaveApi = {
  // 获取闲置买卖列表
  getList(params: SearchParams): Promise<ApiResponse<PageData<LeavePost>>> {
    return request.get('/leave/list', { params })
  },
  
  // 获取闲置买卖详情
  getDetail(id: string): Promise<ApiResponse<LeavePost>> {
    return request.get(`/leave/detail/${id}`)
  },
  
  // 发布闲置买卖
  create(data: Partial<LeavePost>): Promise<ApiResponse> {
    return request.post('/leave/create', data)
  },
  
  // 更新闲置买卖
  update(id: string, data: Partial<LeavePost>): Promise<ApiResponse> {
    return request.put(`/leave/update/${id}`, data)
  },
  
  // 删除闲置买卖
  delete(id: string): Promise<ApiResponse> {
    return request.delete(`/leave/delete/${id}`)
  }
}

// 新闻API
export const newsApi = {
  // 获取新闻列表
  getList(params: SearchParams): Promise<ApiResponse<PageData<NewsPost>>> {
    return request.get('/news/list', { params })
  },
  
  // 获取新闻详情
  getDetail(id: string): Promise<ApiResponse<NewsPost>> {
    return request.get(`/news/detail/${id}`)
  }
}

// 评论API
export const commentApi = {
  // 获取评论列表
  getList(postId: string, params?: SearchParams): Promise<ApiResponse<PageData<Comment>>> {
    return request.get(`/comment/list/${postId}`, { params })
  },
  
  // 添加评论
  create(data: Partial<Comment>): Promise<ApiResponse> {
    return request.post('/comment/create', data)
  },
  
  // 删除评论
  delete(id: string): Promise<ApiResponse> {
    return request.delete(`/comment/delete/${id}`)
  }
}

// 收藏API
export const favoriteApi = {
  // 获取收藏列表
  getList(params: SearchParams): Promise<ApiResponse<PageData<any>>> {
    return request.get('/favorite/list', { params })
  },
  
  // 添加收藏
  add(postId: string, postType: string): Promise<ApiResponse> {
    return request.post('/favorite/add', { postId, postType })
  },
  
  // 取消收藏
  remove(postId: string): Promise<ApiResponse> {
    return request.delete(`/favorite/remove/${postId}`)
  }
}

// 文件上传API
export const uploadApi = {
  // 上传图片
  uploadImage(file: File): Promise<ApiResponse<{ url: string }>> {
    const formData = new FormData()
    formData.append('file', file)
    return request.post('/upload/image', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  }
}

// 搜索API
export const searchApi = {
  // 综合搜索
  search(params: SearchParams): Promise<ApiResponse<PageData<any>>> {
    return request.get('/search', { params })
  }
}